#include <gtest/gtest.h>
#include <iostream>
#include "filter/ecu_filter.h"

// 测试固件类
class FilterTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 测试前的设置
        config.type = FILTER_TYPE_MAV;
        config.order = 5;
        config.coefficients = nullptr;
        config.coefficient_count = 0;
    }
    
    void TearDown() override {
        // 测试后的清理
    }
    
    filter_config_t config;
};

// 测试用例
TEST_F(FilterTest, CreateDestroy) {
    filter_handle_t* filter = filter_create(&config);
    EXPECT_NE(filter, nullptr);
    EXPECT_EQ(filter_get_type(filter), FILTER_TYPE_MAV);
    EXPECT_EQ(filter_get_order(filter), 5);
    filter_destroy(filter);
}

TEST_F(FilterTest, InvalidParameters) {
    // 测试无效参数
    filter_config_t invalid_config = {FILTER_TYPE_MAV, 0, nullptr, 0};
    filter_handle_t* filter = filter_create(&invalid_config);
    EXPECT_EQ(filter, nullptr);
    
    filter = filter_create(nullptr);
    EXPECT_EQ(filter, nullptr);
}

TEST_F(FilterTest, MovingAverageFilter) {
    filter_handle_t* filter = filter_create(&config);
    ASSERT_NE(filter, nullptr);
    
    // 测试移动平均滤波
    float result = filter_update(filter, 1.0f);
    EXPECT_FLOAT_EQ(result, 1.0f);
    
    result = filter_update(filter, 2.0f);
    EXPECT_FLOAT_EQ(result, 1.5f);
    
    result = filter_update(filter, 3.0f);
    EXPECT_FLOAT_EQ(result, 2.0f);
    
    filter_destroy(filter);
}

TEST_F(FilterTest, FilterReset) {
    filter_handle_t* filter = filter_create(&config);
    ASSERT_NE(filter, nullptr);
    
    // 先更新一些数据
    filter_update(filter, 1.0f);
    filter_update(filter, 2.0f);
    
    // 重置滤波器
    filter_reset(filter);
    
    // 重置后应该重新开始计算
    float result = filter_update(filter, 5.0f);
    EXPECT_FLOAT_EQ(result, 5.0f);
    
    filter_destroy(filter);
}

TEST_F(FilterTest, NullPointerSafety) {
    // 测试空指针安全性
    filter_update(nullptr, 1.0f);
    filter_reset(nullptr);
    filter_destroy(nullptr);
    
    filter_type_t type = filter_get_type(nullptr);
    EXPECT_EQ(type, FILTER_TYPE_MAV);
    
    uint16_t order = filter_get_order(nullptr);
    EXPECT_EQ(order, 0);
}

// 性能测试
TEST_F(FilterTest, PerformanceTest) {
    filter_handle_t* filter = filter_create(&config);
    ASSERT_NE(filter, nullptr);
    
    const int ITERATIONS = 10000;
    auto start_time = std::chrono::high_resolution_clock::now();
    
    for (int i = 0; i < ITERATIONS; ++i) {
        filter_update(filter, static_cast<float>(i));
    }
    
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
    
    // 确保单次滤波操作在合理时间内完成
    EXPECT_LT(duration.count(), ITERATIONS * 100); // 每操作不超过100微秒
    
    filter_destroy(filter);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    
    // 设置测试相关配置
    ::testing::GTEST_FLAG(filter) = "*";
    
    int result = RUN_ALL_TESTS();
    
    // 生成测试报告
    std::cout << "=== 滤波算法模块单元测试完成 ===" << std::endl;
    std::cout << "测试用例数: " << ::testing::UnitTest::GetInstance()->total_test_count() << std::endl;
    std::cout << "通过用例数: " << ::testing::UnitTest::GetInstance()->successful_test_count() << std::endl;
    std::cout << "失败用例数: " << ::testing::UnitTest::GetInstance()->failed_test_count() << std::endl;
    
    return result;
}